import StrTo from "./StrTo";
import Row from"./Row.js"

export  default class Blocks{
    constructor(){
        this.list=[];
        this.length=0;
    }
    addRow(row){
        if(this.list.length==0){
            this.list.push(new Block())
            this.list[0].push(row)
        }else {
            this.list[ this.list.length-1].push(row);
        }
    }
    findBlock(n){
        return this.list[n-1]
    }
    /**
     *
     *  从第一个Block中读取 row
     *  从1 记数
     * @param n
     * @returns {*}
     */

    addBlock(){
        this.list.push(new Block());
        this.length=this.list.length;
    }

    //编码
    encoded(){
        let res=[]
        let num=[];
        for(let i=0;i<this.list.length;i++){
            let block=this.list[i].encoded();
            num.push( ...  block );
            num.push("c");
        }
        let arr=[];
        for (let i = 0; i < num.length; i++) {
            let num1 = parseInt(num[i], 16).toString(2);
            arr.push((Array(4).join(0) + num1).slice(-4))
        }

        let buff=StrTo.toBuff(arr);
        let length=0;
        while (buff.byteLength-1250*length>0){
            let uint8array1=new Uint8Array(1250);
            uint8array1.set(buff.subarray(length*1250, length*1250+1250))
            res.push(uint8array1 )
            length++;
        }

        return res;
    }
    //解码
    decode(worksData){

        let _this=this;
        let rows=[];
        for(let l=0;l<worksData.length;l++){
            let workData=worksData[l];
            for(let i=0;i<workData.length;i++){
                let num=workData[i].toString(2);
                let buffNum =  (Array(8).join(0) + num).slice(-8);
                let code0= parseInt(buffNum.substr(0,4),2).toString(16);
                let code1= parseInt(buffNum.substr(4,7),2).toString(16);
                if(code0=='b'){
                    let row=new Row()
                    row.decode(rows);
                    _this. addRow(row)
                    rows=[];
                }else if(code0=='c'){
                    let row=new Row()
                    row.decode(rows);
                    _this. addRow(row)
                    rows=[];

                    _this.addBlock();
                }else {
                    rows.push(code0)
                }

                if(code1=="b"){
                    let row=new Row()
                    row.decode(rows);
                    _this. addRow(row)
                    rows=[];
                }else if(code1=="c"){
                    let row=new Row()
                    row.decode(rows);
                    _this. addRow(row)
                    rows=[];
                    _this.addBlock();
                }else {
                    rows.push(code1)
                }
            }
        }

        if(rows.length>0){
            let row=new Row()
            row.decode(rows);
            _this. addRow(row)
            rows=[];
        }
    }

}

class  Block{
    constructor(){
        this.list=[];
        this.length=0;
    }
    push(row){
        this.list.push(row);
        this.length++;
    }
    findRow(n){
        return this.list[n-1];
    }

    encoded(){
        let num=[];
        for(let i=0;i<this.list.length;i++){
            num.push( ...( this.list[i].encoded()));
            num.push("b");

        }
        num.length=num.length-1;
        return num;
    }
}